// pages/coupon/coupon.js
let myRequest = require('../../common/script/myRequest.js');
let api = require('../../common/script/api.js');
let login = require('../../common/script/login');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEnd: true,
    isWithout: true,
    page: 1, // 页码
    pageSize: 10,
    listData: [],
    status: 1
  },
  navClick(e) {
    let that = this;
    if (e.target.dataset.status == that.data.status) {
      return;
    }
    that.setData({
      status: e.target.dataset.status,
      page: 1,
      isEnd: true,
      isWithout: true,
    })
    that.getData()
  },
  getData() {
    const that = this;
    let data = {
      token: wx.getStorageSync('token'),
      page: that.data.page,
      page_size: that.data.pageSize,
      status: parseInt(that.data.status)
    }
    that.setData({
      listData: []
    })
    // wx.showLoading({
    //   title: '加载中...',
    // })
    myRequest.EasyHttp.post(api.reqUrls.getCouponList, data).then(res => {
      // wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.data.status == 1) {
        if (res.data.result.list.length == 0) {
          that.setData({
            isWithout: false,
            listData: res.data.result.list
          })
        } else {
          that.setData({
            isWithout: true,
            listData: res.data.result.list
          })
        }
      } else {
        that.setData({
          listData: [],
          isWithout: false
        })
      }
     
    })
  },
  toUseCoupon(e) {
    const that = this;
    wx.navigateTo({
      url: `/package/pages/useCoupon/useCoupon?id=${e.target.dataset.id}&status=${that.data.status}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // const that = this;
    // login.checkLogin().then(res => {
    //   let refreshCoupon = wx.getStorageSync('refreshCoupon');
    //   if (!refreshCoupon) {
    //     that.getData()
    //   }

    // }).catch(err => {
    //   app.appCallBack = (e) => {
    //     if (!refreshCoupon) {
    //       that.getData()
    //     }
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const that = this;
    // let refreshCoupon = wx.getStorageSync('refreshCoupon');
    // if (refreshCoupon) {
    //   that.setData({
    //     status: 1
    //   })
    //   wx.setStorageSync('refreshCoupon', false)
    // that.getData()
    // }
    login.checkLogin().then(res => {
      that.getData()
    }).catch(err => {
      app.appCallBack = (e) => {
        that.getData()
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const that = this;
    wx.showNavigationBarLoading()
    that.setData({
      page: 1,
      isEnd: true,
      isWithout: true,
    })
    that.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    if (!that.data.isEnd) {
      return
    }
    let data = {
      token: wx.getStorageSync('token'),
      page: that.data.page + 1,
      page_size: that.data.pageSize,
      status: parseInt(that.data.status)
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.getCouponList, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        if (res.data.result.list.length == 0) {
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            isEnd: false
          })
        } else {
          that.setData({
            listData: [...that.data.listData, ...res.data.result.list],
            page: that.data.page + 1
          })
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})